import plugin from '../../lib/plugins/plugin.js'
import {
    segment
}
    from 'oicq'
import fetch from "node-fetch";

let url = ""

export class zcx20221101 extends plugin {
    constructor() {
        super({
            /** 功能名称 */
            name: '查询快递',
            /** 功能描述 */
            dsc: '',
            event: 'message',
            priority: 1145,
            rule: [
                {
                    reg: "^#*查快递(.*)$",
                    fnc: 'cxkd'
                }
            ]

        })
    }

    async cxkd(e) {
        let danHao = e.msg.substr(4, e.msg.length)
        danHao = Number(danHao);
        console.log(danHao);
        let img = []
        url = `http://tfkapi.top/API/kuaidicha.php?num=${danHao}`;
        console.log(url);
        let response = ""; //调用接口获取数据
        let res = ""; //结果json字符串转对象
        try {
            response = await fetch(url);
            res = await response.json();
            if(res.code != 200){
                e.reply('快递查询失败')
            }else{
                let msg = [
                    "快递公司:",segment.text(res.name),"\n",
                    "快递单号:",segment.text(res.dh),"\n",
                    "快递详情信息：",segment.text(res.msg),"\n",
                ];
                await e.reply(msg);
            }
        } catch {
            return
        }
    }

}